package org.example.string;

public class Invert_words_in_a_string {
    public static void main(String[] args) {
        String s = "  hello world  ";
        //当字符串中含有非空格字符和多个空格字符时，调用split(" ")函数时将得到非空格字符和空字符串的一个数组
        //当字符传中只含有空格字符串时,调用split(" ")函数时将得到一个空数组
        String[] s1 = s.split(" ");
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = s1.length-1; i >= 0; i--) {
            if (s1[i].equals("")){
                continue;
            }
            if (stringBuilder.length()==0){
                stringBuilder.append(s1[i]);
            }else {
                stringBuilder.append(" ").append(s1[i]);
            }
        }
        String s2 = stringBuilder.toString();
        System.out.println(s2);
    }
}
